import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from DBUtils import DBUtils
from LoginOperation import LoginOperation


class RegisterOperation:
    def register(self, driver, username, pwd, repwd, usertype, sex, hobby, email, introduction):
        # 输入用户名
        driver.find_element(By.XPATH, '//*[@id="username"]').send_keys(username)
        # 输入登陆密码
        driver.find_element(By.XPATH, '//*[@id="password"]').send_keys(pwd)
        # 输入确认密码
        driver.find_element(By.XPATH, '//*[@id="surePassword"]').send_keys(repwd)
        # 选择用户类型
        select_element = driver.find_element(By.XPATH, '//*[@id="userType"]')
        # 创建一个Select对象
        select = Select(select_element)
        # 选择下拉列表中的一项
        select.select_by_visible_text(usertype)
        # 选择用户性别
        if sex == '男':
            driver.find_element(By.XPATH, '//*[@id="loginDiv"]/form/div/input[1]').click()
        else:
            driver.find_element(By.XPATH, '//*[@id="loginDiv"]/form/div/input[2]').click()
        # 选择兴趣爱好
        if hobby == 1:
            driver.find_element(By.XPATH, '//*[@id="loginDiv"]/form/p[5]/input[1]').click()
        elif hobby == 2:
            driver.find_element(By.XPATH, '//*[@id="loginDiv"]/form/p[5]/input[2]').click()
        elif hobby == 3:
            driver.find_element(By.XPATH, '//*[@id="loginDiv"]/form/p[5]/input[3]').click()
        elif hobby == 4:
            driver.find_element(By.XPATH, '//*[@id="loginDiv"]/form/p[5]/input[1]').click()
            driver.find_element(By.XPATH, '//*[@id="loginDiv"]/form/p[5]/input[3]').click()
        elif hobby == 5:
            driver.find_element(By.XPATH, '//*[@id="loginDiv"]/form/p[5]/input[2]').click()
            driver.find_element(By.XPATH, '//*[@id="loginDiv"]/form/p[5]/input[3]').click()
        elif hobby == 6:
            driver.find_element(By.XPATH, '//*[@id="loginDiv"]/form/p[5]/input[1]').click()
            driver.find_element(By.XPATH, '//*[@id="loginDiv"]/form/p[5]/input[2]').click()
            driver.find_element(By.XPATH, '//*[@id="loginDiv"]/form/p[5]/input[3]').click()
        # 填写电子邮件
        driver.find_element(By.XPATH, '//*[@id="loginDiv"]/form/p[6]/input').send_keys(email)
        # 跳转框架页
        driver.switch_to.frame('info')
        # 填写自我介绍
        driver.find_element(By.XPATH, '//*[@id="introduceText"]').send_keys(introduction)
        # 退出框架页
        driver.switch_to.default_content()
        # 点击提交按钮
        driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        # 等待弹窗出现
        alert = driver.switch_to.alert
        # 点击弹窗中的确定按钮
        alert.accept()

    # 获取注册成功的实际结果
    def register_result(self, driver, username, pwd):
        login = LoginOperation()
        login.login(driver, username, str(int(pwd)))
        result = login.getSuccessResult(driver)
        return result
